//------------------------------------------------------------------------------//          .
//		Instituto Tecnológico de Costa Rica       //        .  `.
//		IC-6400 Investigación de Operaciones   //      .  `.  `.
//										//    .  `.  `.  `v
//		Prof. Francisco J. Torres Rojas		      //  .  `.  `.  `v  `v
//										//.  `.  `.  `v  `v `V
//		Estudiantes:						// `.  `.  `v  `v  `V
//					Luis A. Cubillo Soto        //   `.  `v  `v  `V
//					200963696		       //       `v  `V
//					Leonel J. Murillo Retana //         `V
//					200900572		       // ____  _____ ____
// 										// |_   _| ____/  ___|
// 		III Proyecto: 						 //   |  |  |  _|   | |
// 				Transportes		                //   |  |  | |__  | | ___
//------------------------------------------------------------------------------//   |_ | |_____\____|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Structures.h"
#include "Matrix.h"
#include "Global.h"

/*

int main(int argc, char** argv) {

        printf("Ingrese la cantidad de productores\n");
        scanf("%d", &PRODUCERS);

        printf("Ingrese la cantidad de consumidores\n");
        scanf("%d", &CONSUMERS);

        int i;
        int j = 1;

        MAX_PRODUCTION = (int*)malloc(80*sizeof(int));
        CONSUMER_NEEDS = (int*)malloc(80*sizeof(int));
        TRANSPORT_COSTS = (int*)malloc (80*sizeof(int));

        for(i = 0; i < 9; i++){
            TRANSPORT_COSTS[i] = 3;
        }

        //A esto le cambio el orden para que haya exceso de un lado o del otro

        for (i = 0; i < 3; i++){
            MAX_PRODUCTION[i] = j;
            j++;
        }

        for (i = 0; i < 3; i++){
            CONSUMER_NEEDS[i] = j;
            j++;
        }
        
        LENGTH_COSTS = PRODUCERS*CONSUMERS;
        check_balance(MAX_PRODUCTION, CONSUMER_NEEDS);
        STONE = create_mat(PRODUCERS,CONSUMERS);
        valorMinimo(STONE, CONSUMERS, PRODUCERS, CONSUMER_NEEDS, MAX_PRODUCTION);
        menorMatriz(STONE, PRODUCERS,CONSUMERS);
        print_mat(PRODUCERS, CONSUMERS, STONE);        
    return (EXIT_SUCCESS);
}
*/

Stones** create_mat(int producer, int consumer){
    Stones** stone;
    int m,n, cont;
    cont = 0;
    if ((stone = (Stones **)malloc(producer * sizeof(Stones*)))){
		for (n = 0; n< producer; n++){
			if((stone[n] = (Stones *)malloc(consumer * sizeof(Stones)))){
				for(m = 0; m < consumer; m++){
					stone[n][m].margin = 0;
					stone[n][m].transportation = TRANSPORT_COSTS[cont];
                                        stone[n][m].stone = 0;
                                        stone[n][m].visited=0;
                                        cont++;
				}
			}
			else{
                            printf("La cantidad de i demasiado grande, se cae");
			}
		}
	}
	else{
            printf("La cantidad de i demasiado grande, se cae");
        }
    return stone;
}

void print_mat(int i, int j, Stones** stone){
    int m,n;
    for (m = 0; m < i; m++){
		for(n = 0; n < j; n++){
			printf("marg=%d ", stone[m][n].margin);
			printf("trans = %d ", stone[m][n].transportation);
                        printf("stone = %d ", stone[m][n].stone);
		}
		printf("\n");
	}
}

void print_array(int* array){
    int j;
    j = 0;
    while (array[j] != NULL){
        printf("%d  ", array[j]);
        j++;
    }
}

/*Si answer < 0 = Se necesita más de los que se produce
 * Si answer > 0 = Se produce más de los que se necesita
 * Si es 0 está balanceado
 */
int is_balanced(int* limits ,int* needs){
    int answer, one,  two, count, i;
    one = 0;
    count = length_array(limits);
    for (i = 0; i < count; i++)
        one = one + limits[i];

    two = 0;
    count = length_array(needs);
    for (i = 0; i < count; i++)
        two = two + needs[i];

    answer = one - two;
    return answer;
}

/*Esto lo hace siempre y cuando el array no tenga un 0 dentro
 * de lo contrario croma como los gdes.@
 */
int length_array(int* array){
    int count = 0;
    while (array[count] != NULL)
        count++;
    return count;
}

void check_balance(int*limits, int* needs){

    int bal, pos;
    bal = is_balanced(limits, needs);
    DUMMY = bal; // I dont need you
    if (bal != 0){
        //consumer = fila
        //producer = columna
        //limits-needs
        //balancee
        if (bal > 0){
            //Tengo exceso de producción
            //Necesito agregar columna dummy de consumidores
            pos = length_array(needs);
            needs[pos] = bal;
            dummy_costs(TRANSPORT_COSTS, CONSUMERS);
            CONSUMERS++;
            //Falta que cuando me den los costos de transporte de cada uno lo meta en el array
        }
        else {
            //La demanda es mayor
            //Necesito agregra fila dummy de productores
            pos = length_array(limits);
            limits[pos] = bal * -1 ;
            PRODUCERS++;
        }
        LENGTH_COSTS = PRODUCERS*CONSUMERS;
    }
    else
        printf("balanceado\n");
}

//Given a certain array it inserts a 0 on the given position
void insert_zero(int* array, int pos){
    int aux, aux2;
    aux2 = 0;
    while (array[pos] != '\0'){
        aux =array[pos]; //respaldo
        array[pos] = aux2;
        aux2 = aux;
        pos++;
    }
    array [pos] = aux2;
}

void dummy_costs(int* array, int mat){
    int aux = mat;
    while(array[aux]!='\0'){
        insert_zero(array, aux);
        aux = aux + mat + 1;
    }
}